package touchtrack.view.gesture;

import org.mt4j.MTApplication;
import org.mt4j.components.interfaces.IMTComponent3D;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapAndHoldProcessor.TapAndHoldEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;

import touchtrack.exception.IllegalDuplicationException;
import touchtrack.model.track.TrackInterface;
import touchtrack.view.graph.TrackGraph;
import touchtrack.view.graph.TrackNode;
import touchtrack.view.scene.TrackDetailScene;
import touchtrack.view.scene.TrackGraphScene;

/**
 * TrackGraphGestureListener
 * 
 * @author	MuMe09 team 6
 * 			http://mumetech.wordpress.com/
 */
public class TrackGraphGestureListener implements IGestureEventListener 
{
	private MTApplication app;
	private TrackGraphScene graphScene;
	private TrackGraph graph;
	
	public TrackGraphGestureListener(MTApplication app, TrackGraphScene graphScene)
	{
		this.app = app;
		this.graphScene = graphScene;
		this.graph = graphScene.getGraph();
	}

	@Override
	public boolean processGestureEvent(MTGestureEvent ge)
	{
		// TAP
		if(ge instanceof TapEvent)
		{
			TapEvent te = (TapEvent) ge;
			IMTComponent3D target = te.getTargetComponent();
			float x = te.getLocationOnScreen().getX();
			float y = te.getLocationOnScreen().getY();
			
			switch (te.getTapID())
			{
			case TapEvent.BUTTON_DOWN:
				
				System.out.println("[DEBUG] Tap ("+x+","+y+") detected on " + target);
				
				TrackNode node = graph.searchNode(x, y);
				
				if (node != null)
				{
					node.selected = ! node.selected;
					/*TrackInterface track = node.getTrackInterface();
					try {
						graph.getTrackLoader().loadSimilarTracks(track.getArtistInterface().getName(), track.getTitle());
					} catch (IllegalDuplicationException e) {
						// ignore ^^
					}
					graph.createGraph();*/
				}
				else
				{
					node = graph.searchInfoButton(x, y);
					
					if (node != null && node.selected)
					{
						app.changeScene(new TrackDetailScene(app, graphScene, node));
					}
					else
					{
						node = graph.searchRemoveButton(x, y);
						
						if (node != null && node.selected)
						{
							graph.removeNode(node);
						}
					}
				}
				
				break;
			case TapEvent.BUTTON_UP:
				//System.out.println("Button up state on " + target);
				break;
			case TapEvent.BUTTON_CLICKED:
				//System.out.println("Button clicked state on " + target);
				break;
			default:
				break;
			}
		}
		// TAP AND HOLD
		else if (ge instanceof TapAndHoldEvent)
		{ 
			TapAndHoldEvent te = (TapAndHoldEvent) ge;
			IMTComponent3D target = te.getTargetComponent();
			float x = te.getLocationOnScreen().getX();
			float y = te.getLocationOnScreen().getY();
			
			switch (te.getId())
			{
			case TapAndHoldEvent.GESTURE_DETECTED:

				System.out.println("[DEBUG] Tap and hold ("+x+","+y+") detected on " + target);
				
				TrackNode node = graph.searchNode(x, y);
				
				if (node != null)
				{
					TrackInterface track = node.getTrackInterface();
					try {
						graph.getTrackLoader().loadSimilarTracks(track.getArtistInterface().getName(), track.getTitle());
					} catch (IllegalDuplicationException e) {
						// ignore ^^
					}
					graph.createGraph();
				}
				
				break;
			case TapAndHoldEvent.GESTURE_UPDATED:
				break;
			case TapAndHoldEvent.GESTURE_ENDED:
				break;
			default:
				break;
			}
		}
		// DRAG
		else if (ge instanceof DragEvent)
		{
			DragEvent de = (DragEvent) ge;
			IMTComponent3D target = de.getTargetComponent();
			float fromX = de.getFrom().getX();
			float fromY = de.getFrom().getY();
			float toX = de.getTo().getX();
			float toY = de.getTo().getY();
			
			switch (de.getId())
			{
			case DragEvent.GESTURE_DETECTED:
				
				TrackNode node = graph.searchNode(fromX, fromY);
				
				if (node != null)
				{
					System.out.println("[DEBUG] Drag detected on " + target);
					node.moveParticle(toX, toY);
				}
				
				break;
			case TapAndHoldEvent.GESTURE_UPDATED:
				break;
			case TapAndHoldEvent.GESTURE_ENDED:
				break;
			default:
				break;
			}
		}
		
		return false;
	}
}